<template>
  <div class="login_main">
    <div class="login_form">
      <h3>欢迎注册博客系统</h3>
      <el-form v-model="signUpData">
        <el-form-item prop="username">
          <el-input v-model="signUpData.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon class="el-input__icon"><user /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="signUpData.password"
            type="password"
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="verPassword"
            type="password"
            placeholder="确认密码"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <div class="button">
          <el-button type="primary" @click="handlesignUpData">注册</el-button>
          <el-button type="info" @click="router.push('/login')">返回</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import { signup } from "@/api/user.js";

const router = useRouter();
// 注意这里要设置为响应式数据
const signUpData = ref({
  username: "",
  password: "",
});
const verPassword = ref("");

function handlesignUpData() {
  if (signUpData.value.username == "") {
    ElMessage.error("用户名不能为空");
  } else if (signUpData.value.password == "") {
    ElMessage.error("密码不能为空");
  } else if (signUpData.value.password != verPassword.value) {
    ElMessage.error("密码与确认密码不一致");
  } else {
    signup(signUpData.value)
      .then((response) => {
        let res = response.data;
        if (res.code == 200) {
          ElMessage.success("注册成功，请登录");
          router.push("/login");
        }
      })
      .catch(() => {});
  }
}
</script>

<style>
.login_main {
  /* 这里必须使用url方法包裹起来 */
  background-image: url("@/assets/3.png");
  display: flex;
  height: 100%;
  /* 子元素靠右显示 */
  justify-content: flex-end;
  align-items: center;
}
.login_form {
  /* 如果居中不起作用，需要加上这一句*/
  display: flex;
  background-color: rgb(157, 226, 238);
  /* 设置不同元素之间换行显示*/
  flex-direction: column;
  margin-right: 10%;
  height: 50%;
  width: 30%;
  align-items: center;
  justify-content: center;
}
.el-form {
  width: 300px;
}
.button {
  width: 50%;
  margin-left: 25%;
}
</style>
